import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import ViteCompression from 'vite-plugin-compression'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig(() => {
  return {
    plugins: [
      vue(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        dts: 'src/typings/auto-import.d.ts',
      }),
      Components({
        dts: 'src/typings/components.d.ts',
        resolvers: [
          AntDesignVueResolver({
            importStyle: false, // css in js
          }),
        ],
      }),
      ViteCompression({
        threshold: 10240,
        ext: '.gz', //gz br
        algorithm: 'gzip', //brotliCompress gzip
        deleteOriginFile: false,
      }),
      createSvgIconsPlugin({
        iconDirs: [path.resolve(process.cwd(), 'src/assets/svg-icons')],
        symbolId: `icon-[dir]-[name]`,
        inject: 'body-last',
        customDomId: '__SVG_ICON_LOCAL__',
      }),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/variables.scss";`,
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
      },
      extensions: ['.js', '.jsx', '.ts', '.tsx', '.json', '.vue', '.css'],
    },
    server: {
      host: true,
      port: 8889,
      // cors: true,
      // proxy: {
      //   '/api': {
      //     target: 'http://192.168.2.124:8003',
      //     // target: 'http://172.201.1.41:8000',
      //     changeOrigin: true,
      //     rewrite: path => path.replace(/^\/api/, ''),
      //     // configure: (proxy, options) => { }
      //   },
      // },
    },
    preview: {
      host: true,
      port: 1995,
      proxy: {
        '/api': {
          // target: 'http://192.168.2.124:8003',
          target: 'http://127.0.0.1:10013',
          // target: 'http://172.201.1.41:8000',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
          // configure: (proxy, options) => { }
        },
      },
    },
    build: {
      outDir: 'dist',
      sourcemap: false,
      esbuild: {
        drop: ['console', 'debugger'],
      },
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ['vue', 'vue-router', 'pinia'],
          },
          minifyInternalExports: true,
        },
      },
    },
  }
})
